import home from "./home"
import storeIndex from "./storeIndex"
import rejister from "./rejister"
import log from "./log"
import shop from "./shop"
import order from "./order"
export default {
    baseData: {
        url: "http://localhost:9803/",
        urlList: [{
            name: "dev",
            url: "http://192.168.100.163:9803/",
        }, {
            name: "local",
            url: "http://localhost:9803/",
        }],
        token: ""
    },
    get(url, header) {
        return new Promise((s, f) => {
            try {
                uni.request({
                    "url": url,
                    header: {
                        ...header,
                        Authorization: this.baseData.token
                    },
                    timeout: 3000,
                    success(data) {
                        if (data.statusCode == 200) {
                            s(data.data)
                        } else {
                            f(data)
                        }
                    },
                    fail(e) {
                        f(e)
                    }
                })
            } catch (e) {
                console.log(e);
                throw e
            }
        })


    },
    post(url, data, header) {
        return new Promise((s, f) => {
            uni.request({
                url,
                method: "POST",
                data,
                header: {
                    ...header,
                    Authorization: this.baseData.token
                },
                timeout: 3000,
                success(data) {
                    if (data.statusCode == 200) {
                        s(data.data)
                    } else {
                        f(data)
                    }
                },
                fail(e) {
                    f(e)
                }
            })
        })
    },
    baseGet(uri, header) {
        return this.get(this.baseData.url + uri, header)
    },
    basePost(uri, data, header) {
        return this.post(this.baseData.url + uri, data, header)
    },
    checkBack(name) {
        for (let data of this.baseData.urlList) {
            if (data.name == name) {
                this.baseData.url = data.url
                break
            }
        }
    },
    setToken(token) {
        this.baseData.token = token
    }
    , ...home
    , ...storeIndex
    , ...rejister
    , ...log
    , ...shop
    , ...order
}
